
package SSU;

import SSU.Exceptions.*;

public final class Vectors {
    private Vectors() { }

     /**
     * Add vector a to vector b elelement to element.
     * Vectors must have same size
     *
     * @param   a, b - vectors to sum
     *
     * @return  vector wich elements are sums of elments
     *          a vector and b vector element to element
     * @throws NonEqualeSizeExcepltion if size of first
     * vector nonequal to size of second vector
     */
    public static Vector sum(Vector a, Vector b){
        if (a.size() != b.size())
            throw new NonEqualeSizeExcepltion(a +" != " + b);
        Vector res = new ArrayVector(a.size());
        for(int i = 0; i < a.size(); i++){
            res.set(i, a.get(i) + b.get(i));
        }
        return res;
    }

     /**
     * Calculate the scalar multiplication.
     * Vectors must have the same size.
     *
     * @param   a, b - vectors to multiply
     *
     * @return  the result of scalar mutiplycayion
     * @throws NonEqualeSizeExcepltion if size of first
     * vector nonequal to size of second vector
     */
    public static double scalar(Vector a, Vector b){
       if (a.size() != b.size())
            throw new NonEqualeSizeExcepltion(a +" != " + b);
        double res = 0;
        for(int i = 0; i < a.size(); i++){
            res += a.get(i) + b.get(i);
        }
        return res;
    }

     /**
     * Searchs the number in vector
     *
     * @param   key     what to search
     *
     * @return  the first position of key or -1
     *          if there is no key in vector
     */
    public static int findFirstOf(Vector a, double key){
        int res = -1;
        for(int i = 0; i < a.size(); i++)
            if (a.get(i) == key){
                res = i;
                break;
            }
        return res;
    }
    
     /**
     * Return the average of vector.
     *
     * @return  the average of vector
     */
    public static double average(Vector a) {
        double res = 0;
        for(int i = 0; i < a.size(); i++)
            res += a.get(i);
        return res / a.size();
    }

    /**
     * Multiply vector on number
     *
     * @param   number  vector multiply for
     *
     * @return  result  vector
     */
    public static Vector multiply(Vector a, double number) {
        Vector res = new ArrayVector(a.size());
        for (int i = 0; i < a.size(); i++) {
            res.set(i, a.get(i) * number);
        }
        return res;
    }

     /**
     * Return the minimal element of vector
     *
     * @return  the minimal element of vector
     */
    public static  double minElement(Vector a) {
        double res = a.get(0);
        for (int i = 1; i < a.size(); i++) {
            if (a.get(i) < res)
                res = a.get(i);
        }
        return res;
    }
}
